﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Besture.CmsControl
{
    public class DataField : WebControl
    {
        private string _Field;
        /// <summary>
        /// 列名
        /// </summary>
        public string Field
        {
            get { return _Field; }
            set { _Field = value; }
        }


        private string _DataModelID;
        /// <summary>
        /// 数据模型编号
        /// </summary>
        public string DataModelID
        {
            get { return _DataModelID; }
            set { _DataModelID = value; }
        }
        

        protected override void Render(HtmlTextWriter writer)
        {
            //字段名称未输入
            if (string.IsNullOrEmpty(Field))
            {
                writer.Write("请输入字段名称！");
                return;
            }
            //字段名称不存在
            try
            {
                DataRow row = null;
                //判断是否存在母版
                string pagefile = Page.MasterPageFile;

                //不存在母版
                if (string.IsNullOrEmpty(pagefile))
                {
                    row = ((DataModel)this.Page.FindControl(DataModelID)).Row;
                }
                else
                {
                    //Cms默认页面的占位符ID
                    //if (string.IsNullOrEmpty(ContentPlaceHolder))
                    //{
                    //    ContentPlaceHolder = "ContentPlaceHolder1";
                    //}
                    //repeater = (CmsControl.Repeater)Page.Master.FindControl(ContentPlaceHolder).FindControl(RepeaterID);

                    row = ((DataModel)this.Page.Master.FindControl("ContentPlaceHolder1").FindControl(DataModelID)).Row;
                }

                
                if(row == null)
                {
                    writer.Write("数据不存在，请检查！");
                }
                else
                {
                    string val = row[Field].ToString();
                    writer.Write(val);
                }
                
            }
            catch (Exception)
            {
                writer.Write("字段【" + Field + "】不存在，请检查！");
            }
            
        }
    }
}
